/**
 * 
 */
package lt.vin.currency.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import lt.vin.currency.server.dao.CurrencyDao;
import lt.vin.currency.shared.Rate;
import lt.vin.currency.shared.Source;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.xml.sax.SAXException;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * @author Vincentas
 *
 */
@Singleton
public class LoadRatesServlet extends HttpServlet {
	
	@Inject CurrencyDao currencyDao;
	
	private static final String BANK_CODE_PARAMETER = "bankCode";

	/**
	 * 
	 */
	private static final long serialVersionUID = -9130496245152395324L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String bankCode = req.getParameter(BANK_CODE_PARAMETER);
		int result = HttpServletResponse.SC_BAD_REQUEST;
		
		if (bankCode != null) {
			try {
				result = handleSource(getCurrencyDao().getSource(bankCode), resp);
			} catch (TransformerConfigurationException e) {				
				e.printStackTrace(resp.getWriter());
			} catch (SAXException e) {
				e.printStackTrace(resp.getWriter());
			} catch (ParserConfigurationException e) {
				e.printStackTrace(resp.getWriter());
			} catch (TransformerException e) {
				e.printStackTrace(resp.getWriter());
			} catch (Throwable e) {
				e.printStackTrace(resp.getWriter());
			}
		}
		
		resp.getWriter().flush();
		resp.setStatus(result);
	}
	
	private int handleSource(Source source, HttpServletResponse response) throws TransformerConfigurationException, SAXException, ParserConfigurationException, IOException, TransformerException {
		int result = HttpServletResponse.SC_OK;
		
		if (source != null) {
			SourceReader sourceReader = new SourceReader(source);
			List<Rate> rates = sourceReader.getRates();
			
			response.getWriter().write("Parsed :");
			response.getWriter().write("<ul>");
			for (Rate rate : rates) {
				response.getWriter().write("<li>");
				response.getWriter().write(ToStringBuilder.reflectionToString(rate, ToStringStyle.SHORT_PREFIX_STYLE));
				response.getWriter().write("</li>");
			}
			response.getWriter().write("</ul>");
			
			getCurrencyDao().save(rates);	
		} else {
			result = HttpServletResponse.SC_BAD_REQUEST;
		}	
		
		return result;
	}

	/**
	 * @return the currencyDao
	 */
	public CurrencyDao getCurrencyDao() {
		return currencyDao;
	}

	/**
	 * @param currencyDao the currencyDao to set
	 */
	public void setCurrencyDao(CurrencyDao currencyDao) {
		this.currencyDao = currencyDao;
	}
	
	
}
